package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 地址管理控制器(后台)
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    //从zookeeper中引入地址管理服务
    @Reference
    private AddressService addressService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增地址
     * @param address
     * @return
     */
    @RequestMapping("/addPlace")
    public Result addPlace(@RequestBody Address address){
        try {
            addressService.addPlace(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_LOCATION_FAIL);
        }
        return new Result(true, MessageConstant.ADD_LOCATION_SUCCESS);
    }

    /**
     * 查询所有地址信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Address> addressList = null;
        try {
            addressList = addressService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_LOCATION_FAIL);

        }
        return new Result(true,MessageConstant.QUERY_LOCATION_SUCCESS,addressList);
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete (int id){
        try {
            addressService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_LOCATION_FAIL);

        }
        return new Result(true,MessageConstant.DELETE_LOCATION_SUCCESS);
    }
}
